package com.jams.goodox.activity.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.jams.goodox.R;
import com.jams.goodox.TownApplicationLike;
import com.jams.goodox.activity.serve.OfficialRentingActivity;
import com.jams.goodox.activity.serve.PartyServiceActivity;
import com.jams.goodox.activity.serve.PersonalRentingActivity;
import com.jams.goodox.activity.serve.SurroundingActivity;
import com.jams.goodox.activity.serve.enterprise.OneKeyServiceActivity;
import com.jams.goodox.activity.serve.headhunter.JobListActivity;
import com.jams.goodox.activity.serve.meet.MeetRoomListActivity;
import com.jams.goodox.activity.serve.park.ParkingApplyActivity;
import com.jams.goodox.activity.serve.praise.ComplainPraiseAddActivity;
import com.jams.goodox.activity.serve.repair.RepairActivity;
import com.jams.goodox.activity.serve.report.AddReportActivity;
import com.jams.goodox.activity.serve.tenement.TenementYearActivity;
import com.jams.goodox.activity.serve.visitor.VisitorAddActivity;
import com.jams.goodox.constant.Constant;
import com.jams.goodox.constant.Constants;
import com.jams.goodox.entity.ServerBean;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.UserUtils;

import static com.jams.goodox.activity.serve.PartyServiceActivity.PARTY_SERVICE;
import static com.jams.goodox.activity.serve.PartyServiceActivity.TITTLE_NEWS;
import static com.jams.goodox.activity.serve.enterprise.OneKeyServiceActivity.ONE_KEY_SERVICE_NAME;
import static com.jams.goodox.activity.serve.enterprise.OneKeyServiceActivity.ONE_KEY_SUB_ID;

/**
 * Created by Jin on 2017/2/21.
 * Description
 */
public class ServiceGo {

    //跳转到生活页面(购物）
    public static void gotoProductPage(Context context, final ServerBean serverBean) {
        int subType = serverBean.getSubType();
        Bundle bundle = new Bundle();
        switch (subType) {
            case Constant.SUB_TYPE_SMALL_IMG://商品服务小图
                if (checkValid(serverBean, true, false)) {
                    bundle.putString("subjectId", serverBean.getSubId());
                }
                break;
            case Constant.SUB_TYPE_BIG_IMG://商品服务大图
                if (checkValid(serverBean, true, false)) {
                    bundle.putString("subjectId", serverBean.getSubId());
                }
                break;
            case Constant.SUB_TYPE_H5_FORM://表单服务h5
                ToastUtil.showShort(context, context.getString(R.string.function_is_building));
                break;
            case Constant.SUB_TYPE_APP_FORM://表单服务原生
                gotoAppServerPage(context, serverBean);
                break;
            case Constant.SUB_TYPE_H5_AUTHORITY://h5服务授权
                break;
            case Constant.SUB_TYPE_H5://h5服务不授权
                break;
            case Constant.SUB_TYPE_ONE_KEY_SERVER://一键服务
                bundle.putString(ONE_KEY_SUB_ID, serverBean.getSubId() + "");
                bundle.putString(ONE_KEY_SERVICE_NAME, serverBean.getCustomName() + "");
                qStartActivity(context, OneKeyServiceActivity.class, bundle);
                break;
        }
    }

    //跳转到对应列表
    private static void gotoAppServerPage(Context context_, final ServerBean item) {
        Intent in;
        switch (item.getSubCode()) {
            case Constants.POUR_CODE://投诉表扬
                if (checkValid(item)) {
                    in = new Intent(context_, ComplainPraiseAddActivity.class);
                    in.putExtra("type", 0);
                    in.putExtra("subId", item.getSubId());
                    in.putExtra("title", item.getCustomName());
                    qStartActivity(context_, in);
                }
                break;
            case Constants.REPAIR_CODE://维修
                if (checkValid(item)) {
                    in = new Intent(context_, RepairActivity.class);
                    in.putExtra("subId", item.getSubId());
                    in.putExtra("title", item.getCustomName());
                    in.putExtra("busId", item.getBusId());
                    qStartActivity(context_, in);
                }
                break;
            case Constants.RESPONSE_CODE://报事
                if (checkValid(item)) {
                    in = new Intent(context_, AddReportActivity.class);
                    in.putExtra("type", 1);
                    in.putExtra("subId", item.getSubId());
                    in.putExtra("title", item.getCustomName());
                    in.putExtra("isPersonReport", (item.getSubHandle()).equals("1"));
                    qStartActivity(context_, in);
                }
                break;
            case Constants.EMS_CODE://快递待寄:无快递贷记需求
//                in = new Intent(context, InputExpressActivity.class);
//                in.putExtra("subId", item.getSubId());
//                in.putExtra("title", item.getCustomName());
//                qStartActivity(in);
                break;
            case Constants.VISITOR_CODE://访客通行
                if (checkValid(item)) {
                    in = new Intent(context_, VisitorAddActivity.class);
                    in.putExtra("subId", item.getSubId());
                    in.putExtra("title", item.getCustomName());
                    qStartActivity(context_, in);
                }
                break;
            case Constants.TENEMENT_PAY_CODE://物业缴费
                if (checkValid(item)) {
                    in = new Intent(context_, TenementYearActivity.class);
                    in.putExtra("subId", item.getSubId());
                    in.putExtra("title", item.getCustomName());
                    in.putExtra("busId", item.getBusId());
                    qStartActivity(context_, in);
                }
                break;
            case Constants.AUTONOMY_RULE_CODE://自治规约
                break;
            case Constants.AUTHORITY_CODE://业委会
                break;
            case Constants.AUTONOMY_VOTE_CODE://自治表决
                break;
            case Constants.VOLUNTEER_CODE:// 志愿者
                break;
            case Constants.PROCEDURE_VOTE_CODE:// 议事投票
                break;
            case Constants.EAGLE_EYE_CODE:// 鹰眼
                break;
            case Constants.SMART_DOOR_CODE://智能开门
                break;
            case Constants.FLOW_CODE://流量充值
                break;
            case Constants.PARKING_CODE://停车缴费
                if (checkValid(item, true, false)) {
                    in = new Intent(context_, ParkingApplyActivity.class);
                    in.putExtra("subId", item.getSubId());
                    in.putExtra("busId", item.getBusId());
                    in.putExtra("title", item.getCustomName());
                    in.putExtra("isPersonPark", (item.getSubHandle()).equals("1"));
                    qStartActivity(context_, in);
                }
                break;
            case Constants.MEET_CODE://场地预约
                if (checkValid(item)) {
                    in = new Intent(context_, MeetRoomListActivity.class);
                    in.putExtra("subId", item.getSubId());
                    in.putExtra("busId", item.getBusId());
                    in.putExtra("title", item.getCustomName());
                    qStartActivity(context_, in);
                }
                break;
            case Constants.HeadHunter_Code://人人都是猎头
                qStartActivity(context_, JobListActivity.class);
                break;
            case Constants.Breakfast_Code://营养早餐
            case Constants.Afternoon_Tea_Code://下午茶
            case Constants.Snacks_Code://休闲零食

                break;
            case Constants.Official_Renting_Code://官方租房
                qStartActivity(context_, OfficialRentingActivity.class);
                break;
            case Constants.Renting_Code://转租合租
                qStartActivity(context_, PersonalRentingActivity.class);
                break;
            case Constants.Surrounding_Build_Code://周边楼盘
                qStartActivity(context_, SurroundingActivity.class);
                break;
            case Constants.Party_Member_Code://党员服务
                Intent i = new Intent(context_, PartyServiceActivity.class);
                i.putExtra(TITTLE_NEWS, PARTY_SERVICE);
                qStartActivity(context_, i);
                break;
            default:
                break;
        }
    }

    public static void qStartActivity(Context context, Intent intent) {
        context.startActivity(intent);
    }

    public static void qStartActivity(Context context, Class<? extends Activity> cls) {
        qStartActivity(context, cls, null);
    }

    public static void qStartActivity(Context context, Class<? extends Activity> cls, Bundle bundle) {
        Intent intent = new Intent();
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        intent.setClass(context, cls);
        context.startActivity(intent);
    }

    private static boolean checkValid(ServerBean serverBean) {
        if ((serverBean.getSubHandle()).equals("2")) {//企业服务
            return UserUtils.checkIsCompetent();
        } else {//个人
            if (!UserUtils.isUnLinkCompany()) {
                return true;
            } else {
                ToastUtil.show(TownApplicationLike.getInstance(), R.string.unLink);
                return false;
            }
        }
    }

    /**
     * @param needCheckCompany 企业服务是否要检查权限
     * @param needCheckLink    个人服务是否要检查权限
     * @return 是否通过
     */
    private static boolean checkValid(ServerBean serverBean, boolean needCheckCompany, boolean needCheckLink) {

        if ((serverBean.getSubHandle()).equals("2")) {//企业服务
            return !needCheckCompany || UserUtils.checkIsCompetent();
        } else {//个人
            if (!needCheckLink) {
                return true;
            }
            if (!UserUtils.isUnLinkCompany()) {
                return true;
            } else {
                ToastUtil.show(TownApplicationLike.getInstance(), R.string.unLink);
                return false;
            }
        }
    }
}
